import { defineStore } from "pinia";
import { computed, ref } from "vue";
import { useUserStore } from "@/stores/userStore";
import { delCartApi, getCartListApi, insertCartApi, mergeCartApi } from "@/apis/cart";

export const useCartStore = defineStore(
  "cart",
  () => {
    const userStore = useUserStore();
    const hasToken = computed(() => userStore.userInfo.token);
    // state
    // 购物车列表
    const cartList = ref([]);
    // 更新购物车列表
    const updateCartList = async () => {
      const res = await getCartListApi();
      cartList.value = res.result;
    };
    // 清空购物车
    const clearCart = () => {
      cartList.value = [];
    };
    //添加购物车
    const addCart = async (goods) => {
      if (hasToken.value) {
        await insertCartApi(goods);
        updateCartList();
      } else {
        // 1.判断购物车中是否有该商品
        const findResult = cartList.value.find((item) => item.skuId === goods.skuId);
        // 2.判断结果
        if (!findResult) {
          // 3.没有则保存
          cartList.value.push(goods);
        } else {
          // 4.有则数量加1
          findResult.count += goods.count;
        }
      }
    };
    // 合并购物车
    const mergeCart = async () => {
      const arr = cartList.value.map((item) => {
        return {
          skuId: item.skuId,
          selected: item.selected,
          count: item.count,
        };
      });
      await mergeCartApi(arr)
      //更新购物车列表
      updateCartList()
    };
    // 删除购物车
    const delCart = async (skuId) => {
      if (hasToken.value) {
        await delCartApi([skuId]);
        updateCartList();
      } else {
        // 过滤掉要删除的数据得到新的数组
        cartList.value = cartList.value.filter((item) => item.skuId !== skuId);
      }
    };
    // 单选
    const singleChecked = (skuId, checked) => {
      // 用户选中单选框该数据的选中状态相应进行变化
      cartList.value.forEach((item) => {
        if (item.skuId === skuId) {
          item.selected = checked;
        }
      });
    };
    // 全选
    const allChecked = (checked) => {
      cartList.value.forEach((item) => (item.selected = checked));
    };
    //计算属性
    // 商品总量
    const allCount = computed(() => {
      return cartList.value.reduce((preValue, currentValue) => preValue + currentValue.count, 0);
    });
    // 商品总价
    const allPrice = computed(() => {
      return cartList.value.reduce(
        (preValue, currentValue) => preValue + currentValue.count * currentValue.price,
        0
      );
    });
    // 判断单选框是否全选
    const isAll = computed(() => {
      return cartList.value.every((item) => item.selected);
    });
    // 已选商品的总数量
    const selectedCount = computed(() => {
      return cartList.value
        .filter((item) => item.selected)
        .reduce((preValue, currentValue) => preValue + currentValue.count, 0);
    });
    // 已选商品的总价格
    const selectedPrice = computed(() => {
      return cartList.value
        .filter((item) => item.selected)
        .reduce((preValue, currentValue) => preValue + currentValue.count * currentValue.price, 0);
    });
    return {
      cartList,
      addCart,
      delCart,
      allCount,
      allPrice,
      singleChecked,
      isAll,
      allChecked,
      selectedCount,
      selectedPrice,
      clearCart,
      mergeCart,
      updateCartList
    };
  },
  {
    persist: true,
  }
);
